package dao.impl;

import dao.BaseDao;
import dao.CommentDao;
import entity.Comment;
import util.DateUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl extends BaseDao implements CommentDao {


    @Override
    public List<Comment> getCommentList(int bid) {
        List<Comment> commentList=new ArrayList<>();
        ResultSet rs=null;
        String sql="select `comment`.id as cid, `user`.image as u_image,`user`.nickname as u_name,content,time from `comment`,`user` where `comment`.uid=`user`.id and bid=? ORDER BY time Desc";
        try{
            rs=executeQuery(sql,bid);
            while (rs.next()){
                Comment comment=new Comment();
                comment.setId(rs.getInt("cid"));
                comment.setU_image(rs.getString("u_image"));
                comment.setU_name(rs.getString("u_name"));
                comment.setContent(rs.getString("content"));
                comment.setTime(rs.getString("time"));
                commentList.add(comment);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,rs);
        }


        return commentList;
    }

    @Override
    public int addComment(Comment comment) {
        int i=0;
        String nowTime= DateUtil.now();
        String sql=" insert into `comment` (uid,bid,content,time) VALUES(?,?,?,?)  ";
        try {
            i=executeUpdate(sql,comment.getUid(),comment.getBid(),comment.getContent(),nowTime);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,null);
        }
        return i;
    }
}
